/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package assign2;

import assign2.ListGUI.LoadFileListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.*;

/**
 *
 * @author martyastiadi
 */
public class ListGUI implements Runnable {

    private ArrayList<Product> product = new ArrayList<Product>();
    private ArrayList<Inventory> inventory = new ArrayList<Inventory>();
    JTextArea text = new JTextArea(20, 10);

    public void run() {
        JFrame frame = new JFrame("SCM - Yusup, port @ ");
        //JTextField field = new JTextField();


        text.setText("Load XML file first!!!");
        text.setEditable(false);

        JMenuBar menu1 = new JMenuBar();
        JMenu m_product = new JMenu("Product");
        menu1.add(m_product);
        JMenuItem m_product1 = new JMenuItem("List required items");
        m_product.add(m_product1);
        JMenuItem m_product2 = new JMenuItem("Make Order");
        m_product.add(m_product2);

        JMenu m_print = new JMenu("Print");
        JMenuItem m_print1 = new JMenuItem("Product List");
        m_print.add(m_print1);
        m_print1.addActionListener(new PrintProduct());
        JMenuItem m_print2 = new JMenuItem("Inventory List");
        m_print.add(m_print2);
        m_print2.addActionListener(new PrintItem());
        menu1.add(m_print);

        JMenu m_file = new JMenu("File");
        JMenuItem m_file1 = new JMenuItem("Load File");
        m_file.add(m_file1);
        m_file1.addActionListener(new LoadFileListener());
        JMenuItem m_file2 = new JMenuItem("Save File");
        m_file.add(m_file2);
        m_file2.addActionListener(new SaveFileListener());
        menu1.add(m_file);

        JMenu m_add = new JMenu("Add");
        JMenuItem m_add1 = new JMenuItem("Inventory");
        m_add.add(m_add1);
        m_add1.addActionListener(new AddInven());
        JMenuItem m_add2 = new JMenuItem("Product");
        m_add.add(m_add2);
        m_add2.addActionListener(new AddProd());
        menu1.add(m_add);

        JMenu m_mess = new JMenu("Message");
        JMenuItem m_mess1 = new JMenuItem("Send Message");
        m_mess.add(m_mess1);
        JMenuItem m_mess2 = new JMenuItem("View Message");
        m_mess.add(m_mess2);
        menu1.add(m_mess);

        frame.setJMenuBar(menu1);
//        frame.add(field);
        frame.add(text);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //frame.getContentPane().add(button);
        frame.setSize(300, 400);
        frame.setVisible(true);
        frame.repaint();

    }

    class LoadFileListener implements ActionListener {

        public void actionPerformed(ActionEvent f) {

            try {
                XMLDecoder dec = new XMLDecoder(new BufferedInputStream(new FileInputStream("Inventory.xml")));
                inventory = (ArrayList<Inventory>) dec.readObject();
                //dec.readObject();
                for (Inventory item : inventory) {
                    if (item instanceof Product) {
                        Product prod = (Product) item;
//                        System.out.println(prod.getName());
                        product.add(prod);
                    }
                }
            } catch (IOException e) {
                //System.out.println ("Testing");
                System.exit(1);

            }
        }
    }

    class SaveFileListener implements ActionListener {

        public void actionPerformed(ActionEvent s) {
//           Product prod = new Product();
//           prod.setName("sillyname");
            try {
                XMLEncoder enc = new XMLEncoder(new BufferedOutputStream(new FileOutputStream("Inventory.xml")));
               // inventory = (ArrayList<Inventory>)
//                        enc.writeObject(prod);
//                for (Inventory item : inventory) {
//                    if (item instanceof Product) {
//                        Product prod = (Product) item;
//                        System.out.println(prod.getName());
//                        product.add(prod);
                enc.close();
            } catch (IOException e) {
                //System.out.println("Testing");
                System.exit(1);

            }
        }
    }

    class PrintProduct implements ActionListener {

        public void actionPerformed(ActionEvent pr) {
            String prodList = new String();
            for (Inventory item : inventory) {
                if (item instanceof Product) {
                    Product prod = (Product) item;
//                    System.out.println(prod.getName());
                    prodList = prodList + prod.getName() + "\n";
                    product.add(prod);
                }
            }
            text.setText(prodList);
        }
    }

    class PrintItem implements ActionListener {

        public void actionPerformed(ActionEvent pr) {
            String itList = new String();
            for(int i = 0; i < inventory.size(); ++i)
            {
                itList = itList + inventory.get(i).getName() + " : " + inventory.get(i).getQty() + " : " + inventory.get(i).getResQty() + "\n";
            }
            
            text.setText(itList);
        }
    }

}


